////////////////////////////////////////Funciones para el manejo de ventanas /////////////////////////////////////
var gWf = 0;
var gWi = null;
var gWx = 0;
var gWy = 0;
var gWh = null;

function createWindow( cH, cTi , txt , w , h , l , t, bMo, cFC )
	{
		oCo = id(cH) ;
		// Si ya existe
		if ( cFC == undefined ) cFC = 'destroyWindow(\'' + cH + '\')';
		if ( oCo ) destroyWindow(cH); //return showWindow(cH);
		// Obtiene variables del entorno
		var ie=document.all && !window.opera;
		var nDomclientWidth=document.documentElement && parseInt(document.documentElement.clientWidth) || 100000;
		oBd=(document.compatMode=="CSS1Compat")? document.documentElement : document.body ;
		nDW=(ie)? oBd.clientWidth : (/Safari/i.test(navigator.userAgent))? window.innerWidth : Math.min(nDomclientWidth, window.innerWidth-16);
		nDH=(ie)? oBd.clientHeight: window.innerHeight;
		nMH=Math.max( oBd.offsetHeight , oBd.scrollHeight );

		ht = 17;
		wb = 15;
		if ( w == undefined )  w = 300;
		if ( h  == undefined )  h  = 300;
		// Si no viene el left /top => lo centra
		if ( l == undefined ) l = (nDW - w) / 2;
		if ( t == undefined ) t = ((ie)? oBd.scrollTop : window.pageYOffset) + (nDH - h ) / 2;
		// Crea el contenedor
		gWh  = cH ;
		oCo = newDiv( cH, '', w, h, l, t, "window"); 
		// Crea el titulo
			oTi = newDiv( '', '', null, ht, null, null, "title", oCo); 
		if (oTi.addEventListener)  oTi.addEventListener('mouseup',  function (e) { mouseUpWindow() }, 1);
		else if (oTi.attachEvent) oTi.attachEvent('onmouseup',  function (e) { mouseUpWindow() });
		if (oTi.addEventListener)  oTi.addEventListener('mousedown', function (e) { return mouseDownWindow(e, cH) }, 1);
		else if (oTi.attachEvent) oTi.attachEvent('onmousedown', function (e) { return mouseDownWindow(e, cH) });
		oTi.innerHTML = cTi ;
		// Crea la Botonera
			oBo = newDiv( '', '<a href="javascript:void(0);" id="' + gWh +'_close" onclick="' + cFC + ';" >x</a>', null, ht, null, null, "buttons", oCo); 
		// Crea el body
			oBd = newDiv( cH+ '_inside', txt, null, null, null, null, 'inside', oCo); 
		// Crea el contenedor
		if ( bMo  )
			oFo = newDiv( cH+ '_fondo', '', null, nMH, null, null, 'fondo'); 
		document.body.appendChild(oCo);
		//agregar un close con el Esc
		if (document.addEventListener)  document.addEventListener('keypress', closeFunction, 0);
		else if (document.attachEvent) document.attachEvent('onkeypress', closeFunction );
		// agrega los eventos al body
		if (document.body.addEventListener && !document.body.getAttribute('onmouseup')  )  document.body.addEventListener('mouseup',  function (e) { mouseUpWindow() }, 1);
		else if (document.body.attachEvent) document.body.attachEvent('onmouseup',  function (e) { mouseUpWindow() });
		if (document.body.addEventListener && !document.body.getAttribute('onmousemove')  )  document.body.addEventListener('mousemove', function (e) { return mouseMoveWindow(e) }, 1);
		else if (document.body.attachEvent) document.body.attachEvent('onmousemove', function (e) { return mouseMoveWindow(e) });
		if (document.body.addEventListener && !document.body.getAttribute('onmousedown')  )  document.body.addEventListener('mousedown', function (e) { if ( gWf == 1 &&  typeof e.preventDefault != 'undefined') {e.preventDefault(); } }, 1);
		else if (document.body.attachEvent) document.body.attachEvent('onmousedown', function (e) { if ( gWf == 1 && typeof e.preventDefault != 'undefined') {e.preventDefault();} });
	}
function closeFunction(e)
{
	if (e.keyCode == 27)
	{
		oClose = id( gWh + '_close' );
		oClose.onclick();
	}
}
function showWindow(winId)
{
	oWindow = id( winId );
	oWindow.style.visibility = 'visible';
}
function destroyWindow(winId)
{
	gWf = 0;
	gWi = null;
	oFo = id(winId + "_fondo") ;
	if ( oFo )
	{
		document.body.removeChild(oFo);
	}
	oCo = id(winId) ;
	if ( oCo )
	{
		document.body.removeChild(oCo);
	}
}
function hideWindow(winId)
{
	gWf = 0;
	gWi = null;
	oFo = id( winId + "_fondo");
	if ( oFo ) oFo.style.visibility = 'hidden';
	oWindow = id( winId );
	oWindow.style.visibility = 'hidden';
}
function mouseDownWindow(e, winId)
{
	if (!e) e = window.event;

	gWf = 1;
	gWi = id( winId );
	gWx = e.clientX - gWi.offsetLeft ;
	gWy = e.clientY - gWi.offsetTop;
}
function mouseUpWindow()
{
	gWf = 0;
	gWi = null;
}
function mouseMoveWindow(e)
{
	if ( !gWf || gWi == undefined ) return 0;
	if (!e) e = window.event;
	newX = e.clientX - gWx;
	newY = e.clientY - gWy;
	gWi.style.left = newX+ "px";
	gWi.style.top = newY + "px";
	return 1;
}
